package com.chenjt.netty.three_websocket;

import com.chenjt.netty.two_socket.heartBeat.server.MyServerInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.net.InetSocketAddress;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/8/9 10:21
 */
public class MyServer {

    public static void main(String[] args) throws Exception{
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    // 使用handler针对的是bossGroup相关的一些信息，比如连接进来之后对连接进行一些日志的输出
                    .handler(new LoggingHandler(LogLevel.INFO))
                    // childHandler针对的是workerGroup相关的一些信息
                    .childHandler(new WebSocketChannelInitializer());

            // sync()表示netty会一直在这里等待
            ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(8899)).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
